package com.myebills.ebpsfrontend.cust.maxis;

import com.myebills.ebpsfrontend.cust.controller.*;
import com.myebills.ebpsfrontend.cust.utils.*;
import com.checkfree.isolutions.customer.persist.Account;
import com.checkfree.isolutions.customer.persist.User;
import com.checkfree.isolutions.admin.ObjectId;
import com.checkfree.isolutions.statement.persist.Statement;
import java.util.Vector;
import java.util.*;
import java.text.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


public class CusCorpAcctTelServlet_v2r00 extends EbppxTransactionServlet {

    public void doTransaction(
    HttpServletRequest req,
    HttpServletResponse res)
    throws ServletException {
        try {
            final HttpSession session = req.getSession(false);

            if (null == session)
                throw new IllegalStateException("no session established");

    		final String randomKey = (String) session.getAttribute(EbppxConstants.RANDOM_KEY);

            final EbppxUrlManager urlManager = getUrlManager(req);

            final String sOidAccount   = urlManager.getSecureParameter(EbppxConstants.ACCOUNT_ID);

            final String encryptedStmtIndex = req.getParameter("stmtIndex");
            String stmtIndex = "";

            if(encryptedStmtIndex==null){
				stmtIndex = "0";
			}else{
				stmtIndex = EncryptionHelper.decrypt(encryptedStmtIndex,randomKey);
			}

            if (stmtIndex.length() == 0) {
                stmtIndex = "0";
            }
            final int stmtIdx =  Integer.parseInt(stmtIndex);

            Vector cfiAccounts = new Vector();
            Vector cfiStatements = new Vector();
            Vector cfiAcctExtKeys = new Vector();

            Vector cfiOldAccounts = new Vector();

			int oldAccountDisplayCount = 0;
			String oldAccountDisplay = "N";

            if (sOidAccount.length() == 0){
                final EbppxEcsUser ecsUser = (EbppxEcsUser) session.getAttribute(EbppxConstants.ECS_USER);

                Vector cfiAccts =  ecsUser.getAccounts();
                cfiAcctExtKeys = ecsUser.getAccountExternalKeys();
                cfiOldAccounts = ecsUser.getOldAccounts();

                for (java.util.Enumeration enumAccounts = cfiAccts.elements(); enumAccounts.hasMoreElements();) {
                    final Account cfiAccount = (Account) enumAccounts.nextElement();
                    cfiStatements.addElement(StatementUtil.getCorpStatement(cfiAccount,EbppxCusConstants.STMT_START_MONTH,stmtIdx));
                    cfiAccounts.addElement(cfiAccount);

					if (!AcctDetails.getAcctKey(cfiAccount.getExternalKey()).equals("")) {
						oldAccountDisplayCount++;
					}
                }
            } else {
                final String sOidStatement = urlManager.getSecureParameter(EbppxConstants.STATEMENT_ID);
                if (sOidStatement.length() == 0) {
                    throw new ServletException("Missing statement identifier");
                }
                final Account cfiAccount =  getAccountHome().findById(new ObjectId(EncryptionHelper.decrypt(sOidAccount,randomKey)));
                //CorpStatement cfiStatement = ;
                cfiStatements.addElement(StatementUtil.getCorpStatementById(EncryptionHelper.decrypt(sOidStatement,randomKey)));
                cfiAccounts.addElement(cfiAccount);
                cfiAcctExtKeys.addElement(cfiAccount.getExternalKey());
					if (!AcctDetails.getAcctKey(cfiAccount.getExternalKey()).equals("")) {
						oldAccountDisplayCount++;
					}
            }

			//to check is there any old account number.
			if (oldAccountDisplayCount > 0) {
				oldAccountDisplay = "Y";
			}
            req.setAttribute(EbppxConstants.CFI_ACCOUNTS, cfiAccounts);
            req.setAttribute(EbppxConstants.CFI_STATEMENTS, cfiStatements);
            req.setAttribute("acctExtKeys",cfiAcctExtKeys);
            req.setAttribute("stmtIndex", EncryptionHelper.decrypt(stmtIndex,randomKey));
            req.setAttribute("oldAccounts",cfiOldAccounts);
            req.setAttribute("oldAccountDisplay",oldAccountDisplay);

            session.setAttribute("fromPage","0");

            forwardToMappedUrl(req, res, EbppxConstants.URL_JSP_PARAM);
        }
        catch (Throwable t) {
            throw new ServletException(t.toString(), t);
        }
    }
}